//
//  PolicyFileViewController.m
//  homeland
//
//  Created by SunShine on 14-1-14.
//  Copyright (c) 2014年 xzysun. All rights reserved.
//

#import "PolicyFileDetailViewController.h"
#import "MyConcernDataService.h"

@interface PolicyFileDetailViewController ()
@property (strong, nonatomic) MyConcernDataService *dataService;

@end

@implementation PolicyFileDetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"政策文件详情";
    self.dataService = [MyConcernDataService new];
    [self startLoadingView];
    [self.dataService loadPolicyFileDetailInfo:self.itemId complete:^(id data) {
        if (data == nil || data == (id)[NSNull null]) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"获取数据失败，请稍后重试." delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
            return ;
        }
        [self configView:data];
        [self stopLoadingView];
    }];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Config View
-(void)configView:(NSDictionary *)json
{
    //标题
    self.titleLabel.text = [json objectForKey:@"title"];
    CGFloat titleHeight = [self caculateHeightWithoutPaddingForString:self.titleLabel.text AndFontSize:self.titleLabel.font.pointSize AndWitdh:CGRectGetWidth(self.titleLabel.frame)];
    CGRect titleFrame = self.titleLabel.frame;
    titleFrame.size.height = titleHeight;
    self.titleLabel.frame = titleFrame;
    //信息
    self.indexNoLabel.text = [NSString stringWithFormat:@"索引号:%@", [json objectForKey:@"fileNum"]];
    self.categoryLabel.text = [NSString stringWithFormat:@"主题分类:%@", [json objectForKey:@"type"]];
    self.orgLabel.text = [NSString stringWithFormat:@"发文机构:%@", [json objectForKey:@"publisher"]];
    self.dateLabel.text = [NSString stringWithFormat:@"发文日期:%@", [json objectForKey:@"submitDate"]];
    self.articalNoLabel.text = [NSString stringWithFormat:@"文号:%@", [json objectForKey:@"sy"]];
    self.keyWordLabel.text = [NSString stringWithFormat:@"主题字:%@", [json objectForKey:@"keywords"]];
    CGRect infoFrame = self.infoPanel.frame;
    infoFrame.origin.y = CGRectGetMaxY(self.titleLabel.frame) + 5.0;
    self.infoPanel.frame = infoFrame;
    //正文
    NSString *detail = [json objectForKey:@"htmlDetail"];
    if (detail == nil || detail == (id)[NSNull null]) {
        detail = @"";//异常防御
    }
    self.contentView.scrollView.scrollEnabled = NO;
    self.contentView.delegate = self;
    self.contentView.backgroundColor = [UIColor clearColor];
    self.contentView.opaque = NO;//设置这个才能透明
    [self.contentView loadHTMLString:detail baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
//    self.contentTextView.text = detail;
//    CGFloat contentHeight = [self caculateHeightWithoutPaddingForString:self.contentTextView.text AndFontSize:14.0 AndWitdh:CGRectGetWidth(self.contentTextView.frame)];
//    CGRect contentFrame = self.contentTextView.frame;
//    contentFrame.size.height = contentHeight;
//    contentFrame.origin.y = CGRectGetMaxY(self.infoPanel.frame) + 5.0;
//    self.contentTextView.frame = contentFrame;
//    self.scrollView.contentSize = CGSizeMake(SCREEN_WIDTH, CGRectGetMaxY(contentFrame)+5.0);
}

#pragma mark - WebView Delegate
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    //计算webview的高度
    CGRect frame = webView.frame;
    frame.size.height = 1;
    webView.frame = frame;
    CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
    frame.size = fittingSize;
    frame.origin.y = CGRectGetMaxY(self.infoPanel.frame);
    webView.frame = frame;
    //重新计算滚动高度
    self.scrollView.contentSize = CGSizeMake(SCREEN_WIDTH, CGRectGetMaxY(frame));
}
@end
